package util;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao5 {

    private static final String jdbcUrl = "jdbc:mysql://192.168.10.129:3306/kq";
    private static final String user = "root";
    private static final String password = "root";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL Connector/J 8.0+
            // 或者对于旧版本的MySQL Connector/J
            // Class.forName("com.mysql.jdbc.Driver"); // MySQL Connector/J 5.1.x
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("MySQL JDBC Driver not found.", e);
        }
    }


    public List<User5> query() {
        List<User5> userList = new ArrayList<>();

        try (Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
             PreparedStatement stmt = conn.prepareStatement(
                     "SELECT name,gpa from stuInfo ORDER BY gpa DESC LIMIT 3")) {

            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Float className = rs.getFloat("gpa");
                String count1 = rs.getString("name");

                User5 user = new User5(className, count1);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return userList;
    }
}
